package br.com.emanuelvictor.architectureandroid.app1.fragment.tablet;

import java.util.Calendar;
import java.util.List;

import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.view.MenuItem;
import android.view.View;
import android.widget.Toast;
import br.com.emanuelvictor.architectureandroid.R;
import br.com.emanuelvictor.architectureandroid.app1.entity.Point;
import br.com.emanuelvictor.architectureandroid.app1.fragment.EditPointFragment;
import br.com.emanuelvictor.architectureandroid.app1.fragment.ListFileFragment;
import br.com.emanuelvictor.architectureandroid.app1.fragment.picker.TimePickerFragment;
import br.com.emanuelvictor.architectureandroid.app1.repository.PointRepository;
import br.com.emanuelvictor.architectureandroid.app1.support.DatabaseHelper;
import br.com.emanuelvictor.architectureandroid.app1.util.Export;
import br.com.emanuelvictor.architectureandroid.app1.util.PointVariables;
import br.com.emanuelvictor.architectureandroid.util.Interface;
import br.com.emanuelvictor.architectureandroid.util.Navigation;

public class EditPointTablet extends EditPointFragment {

	@Override
	public void handle(Object obj) throws Exception {
		if (obj instanceof MenuItem) {
			switch (((MenuItem) obj).getItemId()) {
			case R.id.action_add:
				try {
					if (this.calendar == null) {
						this.calendar = Calendar.getInstance();
					}
					PointRepository repository = new PointRepository(
							new DatabaseHelper(getActivity()));
					List<Point> points = repository
							.findAllByDate(this.calendar);
					if (points != null && points.size() >= 4) {
						Toast.makeText(getActivity(), R.string.more_than_four,
								Toast.LENGTH_SHORT).show();
					} else {
						Calendar c = this.calendar;
						c.set(Calendar.HOUR_OF_DAY,
								Calendar.getInstance()
										.get(Calendar.HOUR_OF_DAY));
						c.set(Calendar.MINUTE,
								Calendar.getInstance().get(Calendar.MINUTE));
						Point p = new Point();
						p.date = c;
						repository.insert(p);
						Toast.makeText(getActivity(), R.string.saved,
								Toast.LENGTH_SHORT).show();
						((Interface) getActivity()).update();
					}
				} catch (Exception e) {
					Toast.makeText(getActivity(), R.string.save_error,
							Toast.LENGTH_SHORT).show();
				}
				break;
			case R.id.action_export_json:
				Export.exportToJSON(getActivity());
				break;
			case R.id.action_export_csv:
				Export.exportToCSV(getActivity());
				break;
			case R.id.action_import:
				((Interface) getActivity()).goTo(new ListFileFragment());
				break;
			case android.R.id.home:
				this.home();
				break;
			}
		} else if (obj instanceof View) {
			switch (((View) obj).getId()) {
			case R.id.first_morning:
				showPicker(0);
				break;
			case R.id.second_morning:
				showPicker(1);
				break;
			case R.id.first_afternoon:
				showPicker(2);
				break;
			case R.id.second_afternoon:
				showPicker(3);
				break;
			}
		}
	}

	private void showPicker(int indexEdition) {

		List<Point> points = this.dailyHandler.getPoints();

		if (indexEdition < points.size()) {
			isEditing = true;
		} else if (indexEdition == points.size()) {
			isEditing = false;
		} else {
			return;
		}

		DialogFragment newFragment = new TimePickerFragment();
		Bundle bundle = new Bundle();
		bundle.putBoolean("isEditing", isEditing);
		bundle.putInt("index", indexEdition);
		newFragment.setArguments(bundle);

		newFragment.show(getActivity().getSupportFragmentManager(),
				"timePicker");
	}

	@Override
	public void back() throws Exception {
		if (((PointVariables) ((Navigation) getActivity()).getVariables())
				.getHistoryNavigation().isHome()) {
			this.getActivity().finish();
		} else {
			((PointVariables) ((Navigation) getActivity()).getVariables())
					.getHistoryNavigation().decrementHistory();
			((Interface) getActivity()).update();
		}
	}

	@Override
	public void configureActionBar() {

	}

	@Override
	public void home() throws Exception {
		((PointVariables) ((Navigation) getActivity()).getVariables())
				.getHistoryNavigation().restart();
		((Interface) getActivity()).update();
	}

	@Override
	public void goTo(Object obj) throws Exception {

	}
}
